/*
 * @Author: zhangsai
 * @Date: 2018/1/16
 * @Last Modified by: zhangsai
 * @Last Modified time: 2018/1/16
 * @description: 文件列表 混合
 */

import {api} from './api';
import {STATUS_HTTP_SUCCESS, apiHttp} from '__service/csHttp';
import { openImgInBody } from '__service/utils';
export default {
    data() {
        return {
            fullLoading: false
        }
    },
    methods: {
        // 下载文件
        async downloadFile(id) {
            if (id) {
                window.open(this.baseURL + api.DOWNLOAD_FILE.url + id + '?X-Token=' + this.$store.state.user.token);
            } else {
                this.$message({
                    type: 'danger',
                    message: '下载失败请重试!'
                });
            }
        },
        // 预览文件校验
        async previewFileCheck(id, fileName) {
            let fileTypeList = fileName.split('.');
            let fileType = fileTypeList[fileTypeList.length - 1].toLocaleLowerCase();
            if (fileType === 'gif' || fileType === 'png' || fileType === 'jpg' || fileType === 'jpge') {
                openImgInBody(id, 'img');
                return;
            } else if (fileType === 'txt') {
                let params = {
                    id: id
                }
                let { resCode } = await apiHttp(api.VALIDATE_TXT, params);
                if (resCode === STATUS_HTTP_SUCCESS) {
                    openImgInBody(id, 'txt');
                    return;
                }
            } else if (
                fileType === 'docx' ||
                fileType === 'doc' ||
                fileType === 'pdf' ||
                fileType === 'xls' ||
                fileType === 'xlsx' ||
                fileType === 'ppt' ||
                fileType === 'pptx'
            ) {
                this.fullLoading = true;
                let params = {
                    id: id
                };
                let resData = await apiHttp(api.PREVIEW_FILE_CHECK, params);
                if (resData.resCode === STATUS_HTTP_SUCCESS) {
                    let data = resData.data
                    this.previewFile(data);
                }
                this.fullLoading = false;
            } else {
                this.$message({
                    type: 'warning',
                    message: '该文件类型暂时无法预览!'
                });
                return;
            }
        },
        // 预览文件
        async previewFile(fileName) {
            if (fileName) {
                window.open(this.baseURL + api.PREVIEW_FILE.url + '?X-Token=' + this.$store.state.user.token + '&fileName=' + fileName);
            }
        },
    }
}
